<?php
namespace Badmins\Controller;
use Think\Controller;
class PublicController extends Controller{
	private $path = "./Public/Cache/";
	public function verify(){
		$Verify = new \Think\Verify();
		$Verify->length   = 4;
		$Verify->codeSet = '0123456789';
		$Verify->entry();
	}
	public function login(){
		$this->display('Public/login');
	}
	public function cklogin(){
		if(IS_POST){
			//$list = F('web_web','',$this->path);
			 $ip = get_client_ip();
			/*if(stripos($list['limit_ip'], $ip)===false){
					$this->login();
					echo "<script>$(function(){a('error', '登录失败，你的ip已被系统禁止登录啦')});</script>";exit;
				} 
			} */
			$db = D('Admin');
			$username = I('username');
			$password = I('pwd');
			$verifyy = I('post.verify_code');
			$remember = (int)I('remember');
			$verify = new \Think\Verify();
			$a = $verify->check($verifyy);
			if($a){
				$passwod = $db->autopassword($password);
				$re = $db->where("username='{$username}'  and status='0'")->find();
				if(!empty($re)){
					/* $ip_lock_time = $list['ip_lock_time']?$list['ip_lock_time']:'2';//ip锁定时间
					$chatime = (strtotime(date("Y-m-d H:i:s")) - strtotime($re['fail_time']))/3600;
					$fail_time = $list['fail_num']?$list['fail_num']:'0';//登陆失败次数
					if($chatime>$ip_lock_time && $re['fail_num'] >= $fail_time){
						$db->where("id='{$re['id']}'")->setField('fail_num',0);
						$re['fail_num'] = 0;
					} */
					/* if(!empty($fail_time)){
						if($re['fail_num']>=$fail_time){
							$this->login();
							if($chatime<$ip_lock_time){
								echo "<script>$(function(){a('error', '你登陆的失败次数超出最大登录数啦，系统禁止你登陆')});</script>";exit;
							}
							echo "<script>$(function(){a('error', '你登陆的失败次数超出最大登录数啦，系统禁止你登陆')});</script>";exit;
						}
					} */
					$ree = $db->where("id='{$re['id']}' and password='{$passwod}' and status='0'")->find();
					if(empty($ree)){
						/* $data['fail_time'] = date("Y-m-d H:i:s");
						$data['fail_num'] = $re['fail_num'] +1;
						$data['id'] = $re['id'];
						$db->save($data); */
						/* $this->login();
						echo "<script>$(function(){a('error', '密码输入错误')});</script>";exit; */
					}
					$time = 30;
					session(array('name'=>'bxd_shopping_admin_id','expire'=>($time*60)));
					session('bxd_shopping_admin_id',$re['id']);
					$db->where("id='{$re['id']}'")->setField('fail_num',0);
					$map['type'] = 0;
					$map['uid'] = $re['id'];
					$map['login_ip'] = $ip;
					$map['login_time'] = date("Y-m-d H:i:s");
					M('login_log')->add($map);
					$this->redirect('Index/index');exit();
				}else{
					$this->login();
					echo "<script>$(function(){a('error','账号输入错误')});</script>";exit;
				}
			}else{
				$this->login();
				echo "<script>$(function(){a('error', '验证码输入错误')});</script>";exit;
			}
		}else{
			$this->redirect('Public/login');
		}
	}
	/**
	 * 退出登陆
	 */
	public function outlogin(){
		session('shopping_admin_id',null);
		session_destroy();
		$this->redirect('Public/login');exit();
	}
	/**
	 * 修改密码
	 */
	public function update_password(){
		$id = session('shopping_admin_id');
		if(empty($id)){
			showmsg(300,'请登录');exit;
		}
		$oldpass = I('post.oldpwd');
		$newpwd = I('post.newpwd');
		$newpwdpwd = I('post.newnewpwd');
		$db = D('Admin');
		$oldpass = $db->autopassword($oldpass);
		$re = $db->where("id='{$id}' and password='{$oldpass}'")->find();
		if(empty($re)){
			showmsg(300,'旧密码输入错误');exit;
		}
		if($newpwd!=$newpwdpwd){
			showmsg(300,'两次密码输入不一致');exit;
		}
		if($oldpass == $newpwd){
			showmsg(300,'新密码不能与旧密码一样');exit;
		}
		$newpwd = $db->autopassword($newpwd);
		$re = $db->where("id='{$id}' and password='{$oldpass}'")->setField('password',$newpwd);
		showmsg(200,'修改密码成功');exit;
	}
}